'use strict'

// 爬取数据
const Crawler = require('crawler');
const post = require('../model/post');  //链接数据库
const request = require('request');
var int =0;
const crawler = new Crawler({
    maxConnections: 100,
    callback: function (error, res, done) {
        if (error) {
            console.log(`错误信息:${error}`);
        } else {
            var $ = res.$;
            var list = $('.dw_table .el').toArray();
            list.forEach(element => {
                var txt = $(element);
                var postName = txt.find('p.t1 span').text().trim();
                if (postName == '数据分析师') {
                    var workingPlace = txt.find('.el span.t3').text();
                    var companyName = txt.find('.el span.t2 a').text();
                    var salary = txt.find('.el span.t4').text();
                    var releaseTime = txt.find('.el span.t5').text();
                    var time = new Date().getFullYear();                 
                    var mytime = time + '-' + releaseTime;                
                        // 添加到数据库
                        post.create({
                            postName: postName,
                            companyName: companyName,
                            workingPlace: workingPlace,
                            salary: salary,
                            releaseTime: mytime
                        }).then((row) => {
                            console.log(`添加到数据库的Id为：${row.id}`)
                        })
                }
            });
        }
        done();
    }
});

//把函数暴露出去
module.exports = (url) => {
    crawler.queue(url);
}